import pandas as pd

from src.event.EventBus import Event, EventManager
from src.monitor.BaseMonitor import BaseMonitor


class LogcatMonitor(BaseMonitor):
    CMD = '''adb logcat DualTest:V *:S -b main'''
    CMD1 = '''
    while true; do echo 11-15 00:18:01.844  6511  6778 D DualTest: 0 14.433 14.436; sleep 1; done
    '''
    DUMMY = '''
11-14 21:47:28.555 23786 26113 D DualTest: 1 5.639 6.212
11-14 21:47:28.687 23786 26115 D DualTest: 0 5.302 5.311
11-14 21:47:54.862 23786 26115 D DualTest: 0 1.713 16.497
11-14 21:48:19.534 23786 26113 D DualTest: 1 13.028 40.353
11-14 21:48:42.232 23786 26115 D DualTest: 0 4.734 55.913
11-14 22:37:04.074 23786 26113 D DualTest: 1 5.417 334.511
11-14 22:37:06.289 23786 26115 D DualTest: 0 8.534 282.372
11-14 22:37:15.421 23786 26115 D DualTest: 0 5.666 273.353
11-14 22:37:16.679 23786 26113 D DualTest: 1 10.172 336.003
'''

    def __init__(self, name=''):
        self.df_columns = ['Cam-0-sync',
                           'Cam-0-async',
                           'Cam-1-sync',
                           'Cam-1-async']
        self.name = name
        self.new_row = dict()
        self.data = pd.DataFrame(columns=self.df_columns, dtype=float)
        self.process = None
        self.thread = None
        self.is_alive = False

    def parse_line(self, line):
        elements = line[:-2].split()

        self.new_row['Cam-%s-sync' % elements[6]] = elements[7]
        self.new_row['Cam-%s-async' % elements[6]] = elements[8]
        s = pd.Series(self.new_row, index=self.df_columns, name=elements[1], dtype=float)
        self.data = self.data.append(s)
        EventManager.emit(Event(Event.ON_CHART_UPDATE))